package com.common.util;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

public class FtpUtils {

	private int port=21;
//	private String ftpServer="172.16.0.65";
//	private String user="robin_ftp";
//	private String pwd="123";

//	private String ftpServer="172.16.15.101";
	private String ftpServer="222.209.207.26";
	private String user="ipdtfc_ftp";
	private String pwd="ipdtfc_ftp";
	public FtpUtils()
	{

	}

	public FtpUtils(String ftpServer, String user, String pwd, int port) {
		super();
		this.ftpServer = ftpServer;
		this.user = user;
		this.pwd = pwd;
		this.port=port;
	}

	public static FtpUtils getInstanse() {
			synchronized (FtpUtils.class){
				return new FtpUtils();
			}
	}
	/**
     * Description: 向FTP服务器上传文件
     * @param path FTP服务器保存目录
     * @param filename 上传到FTP服务器上的文件名
     * @param input 输入流
     * @return 成功返回true，否则返回false
     */
	public boolean uploadFile(String path,String filename,InputStream input)
	{
		boolean result=false;
		int reply;
		FTPClient ftp=new FTPClient();
		try {
			// 连接FTP服务器
			if (port > -1) {
				ftp.connect(this.ftpServer, port);
			} else {
				ftp.connect(this.ftpServer);
			}
			// 登录FTP
			ftp.login(this.user, this.pwd);

			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return result;
			}
			ftp.changeWorkingDirectory(path);
			ftp.setControlEncoding("UTF-8"); // 中文支持
			ftp.setFileType(FTP.BINARY_FILE_TYPE);
			ftp.storeFile(filename, input);
			input.close();
			ftp.logout();
			result = true;

		} catch (IOException e) {
			e.printStackTrace();
//			 throw new RuntimeException(e);
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException e) {
					 throw new RuntimeException(e);

				}
			}
		}
		return result;
	}

	/**
	 * Description: 从FTP服务器下载文件
	 * @param remotepath
	 * @param filename
	 * @param ous
	 * @return
	 */
	public boolean downloadFile(String remotepath,String filename,OutputStream  ous)
	{
		boolean result=false;
		int reply;

		FTPClient ftp = new FTPClient();
		try {
			if (port > -1) {
				ftp.connect(this.ftpServer, port);
			} else {

				ftp.connect(this.ftpServer);

			}
			ftp.login(this.user, this.pwd);//登录
			ftp.setControlEncoding("UTF-8"); // 中文支持
			ftp.setFileType(FTP.BINARY_FILE_TYPE);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(remotepath);//转移到FTP服务器目录
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs)
            {
                if (ff.getName().equals(filename))
                {

                    ftp.retrieveFile(ff.getName(), ous);
                    ous.close();
					ftp.logout();
					break;
                }
            }
            result=true;
		} catch (SocketException e) {
			 throw new RuntimeException(e);
		} catch (IOException e) {
			 throw new RuntimeException(e);
		}
		finally
		{
			 if (ftp.isConnected())
	            {
	                try
	                {
	                    ftp.disconnect();
	                }
	                catch (IOException e)
	                {
	                	 throw new RuntimeException(e);
	                }
	            }
		}
		return result;
	}

	/**
	 * Description: 从FTP服务器删除文件
	 * @param remotepath
	 * @param filename
	 * @return
	 */
	public boolean deleteFile(String remotepath,String filename)
	{
		boolean result=false;
		FTPClient ftp = new FTPClient();
        try
        {
            int reply;

            // 连接FTP服务器
            if (port > -1)
            {
                ftp.connect(this.ftpServer, port);
            }
            else
            {
                ftp.connect(this.ftpServer);
            }

            // 登录
            ftp.login(this.user, this.pwd);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return result;
            }

            // 转移到FTP服务器目录
            ftp.changeWorkingDirectory(remotepath);
            result = ftp.deleteFile(remotepath + "/" + filename);
            ftp.logout();
        }
        catch (IOException e)
        {
            result = false;
            throw new RuntimeException(e);
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException e)
                {
                	 throw new RuntimeException(e);
                }
            }
        }
		return result;
	}
	public String getFtpServer() {
		return ftpServer;
	}

	public void setFtpServer(String ftpServer) {
		this.ftpServer = ftpServer;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}


}
